﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using RoboContainer.Core;
using TimeMachine.Engine;
using TimeMachine.Engine.Reports;

namespace MetricsAgent
{
	public class ReportsAgent
	{
		private readonly IProjectSettings projectSettings;

		public ReportsAgent(IProjectSettings projectSettings)
		{
			this.projectSettings = projectSettings;
		}

		public void Run()
		{
			var container = new Container(
				c =>
					{
						c.ForPlugin<IProjectSettings>().UseInstance(projectSettings);
						c.ForPlugin<TimeRange>().UseInstance(new TimeRange(DateTime.MinValue, DateTime.MaxValue));

					});
			var worstCodeReport = container.Get<WorstCodeReport>();
			IEnumerable<FileStat> fileStats = worstCodeReport.Calculate();
			string reportFile = Path.Combine(projectSettings.GetDataDirPath(), "worstCode.xml");
			using(var s = new FileStream(reportFile, FileMode.Create))
				new XmlSerializer(typeof(Report)).Serialize(s, new Report {Stats = fileStats.ToArray()});
		}

		public class Report
		{
			public FileStat[] Stats { get; set; }
		}
	}
}